<template>
	<section>

		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item v-if="roles && roles == 'root'">
					<el-button type="primary"  @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--价格列表-->
		<el-table
				:data="priceList.filter(data => !search || data.typeName.toLowerCase().includes(search.toLowerCase()))"
				border
				style="width: 100%;"
				v-loading="listLoading"
		>
			<el-table-column prop="typeName" label="名称" min-width="120" sortable>
			</el-table-column>
			<el-table-column prop="bottomPrice" label="最低" min-width="160" sortable>
				<template slot-scope="scope">
					<el-input-number v-model="scope.row.bottomPrice" controls-position="right"  :min="1"></el-input-number>
				</template>
			</el-table-column>
			<el-table-column prop="topPrice" label="最高" min-width="160" sortable>
				<template slot-scope="scope">
					<el-input-number v-model="scope.row.topPrice" controls-position="right"  :min="1" ></el-input-number>
				</template>
			</el-table-column>
			<el-table-column prop="percent" label="百分比" min-width="160" sortable>
				<template slot-scope="scope">
					<el-input-number  v-model="scope.row.percent" controls-position="right"  :min="0" :max="1"></el-input-number>
				</template>
			</el-table-column>
			<el-table-column prop="days" label="天数" min-width="160" sortable>
				<template slot-scope="scope">
					<el-input-number v-model="scope.row.days" controls-position="right"  :min="1"></el-input-number>
				</template>
			</el-table-column>
			<el-table-column prop="startTime" label="开抢时间" min-width="100" sortable>
			</el-table-column>
			<el-table-column prop="lowestProfit" label="盈利最低(天)" min-width="140" sortable>
				<template slot-scope="scope">
					￥{{(scope.row.bottomPrice * scope.row.percent / scope.row.days).toFixed(2)}}
				</template>
			</el-table-column>
			<el-table-column prop="highestProfit" label="盈利最高(天)" min-width="140" sortable>
				<template slot-scope="scope">
					￥{{(scope.row.topPrice * scope.row.percent / scope.row.days).toFixed(2)}}
				</template>
			</el-table-column>
			<el-table-column label="操作" width="200">
				<template slot="header" slot-scope="scope">
					<el-input
							v-model="search"
							size="mini"
							placeholder="输入名字搜索"/>
				</template>
				<template slot-scope="scope">
					<!--<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="名称" prop="typeName">
					<el-input v-model="addForm.typeName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="最低价" prop="bottomPrice">
					<el-input-number v-model="addForm.bottomPrice" :min="1" auto-complete="off"></el-input-number>
				</el-form-item>
				<el-form-item label="最高价" prop="topPrice">
					<el-input-number v-model="addForm.topPrice" :min="1" auto-complete="off"></el-input-number>
				</el-form-item>
				<el-form-item label="百分比" prop="percent">
					<el-input-number v-model="addForm.percent" :min="0.01" :step="0.01" auto-complete="off"></el-input-number>
				</el-form-item>
				<el-form-item label="天数">
					<el-input-number v-model="addForm.days" :min="1" auto-complete="off"></el-input-number>
				</el-form-item>
				<el-form-item label="开抢时间">
					<el-time-picker
							value-format="hh:mm"
							v-model="addForm.startTime"
							placeholder="选择时间">
					</el-time-picker>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import {addPrice,getPriceList,delPrice} from "../../api/price/api";

	export default {
		data() {
			return {
				addFormVisible:false,
				addLoading:false,
				search:'',
				priceList: [],
				listLoading: false,
				roles: JSON.parse(sessionStorage.getItem('user')).roles,
				addForm: {
					startTime:'', //开抢时间
					typeName: '', // 名称
					bottomPrice: '',  //最低价
					topPrice: '',   //最高价
					percent: '',  //百分比
					days: '', // 天数
					userId: JSON.parse(sessionStorage.getItem('user'))._id
				},
				addFormRules: {
					typeName: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					],
				},
			}
		},
		methods: {
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { id: row._id };
					delPrice(para).then((res) => {
						this.listLoading = false;
						this.getPriceList();
					});
				}).catch(() => {

				});
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
			},
			getPriceList(){
				getPriceList({}).then((res) => {
					if(res.code == 200) {
						this.priceList = res.data
					}
				});
			},
			addSubmit(){
				this.$confirm('确认提交吗？', '提示', {}).then(() => {
					this.addLoading = true;
					if(!this.addForm.typeName){
						this.$message.error('请输入名称！');
						return false
					}
					let para = Object.assign({}, this.addForm);
					addPrice(para).then((res) => {
						if(res.code == 200) {
							this.addLoading = false;
							this.addFormVisible = false;
							this.getPriceList();
						}
					});
				});
			}
		},
		mounted() {
			this.getPriceList()
		}
	}

</script>

<style scoped>

</style>
